<?php
	session_start();

	$response_count;
	$response_id = htmlspecialchars($_GET["response_id"],ENT_QUOTES);

	error_reporting(E_ALL & ~E_DEPRECATED);
	require_once("DB.php");
	require_once("db_connect.php");

	$sql = "SELECT count(*) as response_count FROM response WHERE thread_id = ?";

	$data = htmlspecialchars($_GET["thread_id"],ENT_QUOTES);
	$res =& $db->query($sql, $data);

	if (PEAR::isError($res)) {
		die($res->getMessage());
	}

	while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) {	//SQLの実行結果を表示
		$response_count = $row['response_count'];
	}

	if($response_count==1){
		$sql = "DELETE FROM response WHERE response_id = ?";
		$stmt = $db->prepare($sql);
		$db->execute($stmt,$response_id);
		$sql = "DELETE FROM thread WHERE thread_id = ?";
		$stmt = $db->prepare($sql);
		$db->execute($stmt,$data);
		if (DB::isError($db)) {
		   exit($res->getMessage());
		}
		header("Location: thread.php");
	}else{
		$response_id = htmlspecialchars($_GET["response_id"],ENT_QUOTES);

		$sql = "DELETE FROM response WHERE response_id = ?";
		$stmt = $db->prepare($sql);
		$db->execute($stmt,$response_id);
		if (DB::isError($db)) {
		   exit($res->getMessage());
		}
		header("Location: response.php?thread_id=".htmlspecialchars($_GET["thread_id"],ENT_QUOTES));
	}


?>